-- @owner: zhao-pengcheng-g
-- @date: 2022/01/24
-- @testpoint:分区表，视图，触发器，instead of——更新视图，触发替代触发事件

--step1:建表;expect:成功
drop table if exists table_src_0014_1 cascade;
drop table if exists table_des_0014_2 cascade;
drop sequence if exists test_trigger_des_seq_0014 cascade;
create table table_src_0014_1(id1 int, id2 int, id3 int) with (storage_type=ustore)
partition by range(id1)
(
        partition p1 values less than(1),
        partition p2 values less than(100000),
        partition p3 values less than(maxvalue)
);
create table table_des_0014_2(id1 int, id2 int, id3 int) with (storage_type=ustore)
partition by range(id1)
(
        partition p1 values less than(1),
        partition p2 values less than(100000),
        partition p3 values less than(maxvalue)
);

--step2：创建视图，函数，触发器，expect:成功
create or replace view test_trigger_src_view_0014 as select * from table_src_0014_1;
create sequence test_trigger_des_seq_0014;
create or replace function insert_func_0014()
returns trigger as $$
begin
insert into table_des_0014_2 values(nextval('test_trigger_des_seq_0014'),nextval('test_trigger_des_seq_0014'),nextval('test_trigger_des_seq_0014'));
return new;
end;
$$ language plpgsql;
/
create trigger trigger_table_src_0014_1  instead of update on test_trigger_src_view_0014
for each row
execute procedure insert_func_0014();
/
--step2:更新视图，触发替代触发事件;expect:成功
select * from table_des_0014_2;
insert into table_src_0014_1 values(11,22,33);
update test_trigger_src_view_0014 set id1=123 ;
select * from table_des_0014_2;
select * from test_trigger_src_view_0014;

--step3:环境清理;expect:成功
drop trigger trigger_table_src_0014_1 on test_trigger_src_view_0014 cascade;
drop view test_trigger_src_view_0014;
drop table table_src_0014_1 cascade;
drop table table_des_0014_2 cascade;
drop function insert_func_0014;
drop sequence test_trigger_des_seq_0014 cascade;